<script>
function myFunction() {
   var redirect = confirm("Perhitungan berhasil, Kembali ke halaman sebelumnya");
   if (redirect == true) {
       window.location.href = 'carisesimedian.php'; 
   }
}
</script>

<body onload="myFunction()">

<table>
<?php

ini_set('max_execution_time', 10000);
function calculate_median($arr) {
    sort($arr);
    $count = count($arr); //total numbers in array
    $middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value
    if($count % 2) { // odd number, middle is the median
        $median = $arr[$middleval];
    } else { // even number, calculate avg of 2 medians
        $low = $arr[$middleval];
        $high = $arr[$middleval+1];
        $median = (($low+$high)/2);
    }
    return $median;
}


















session_start();
$host = "localhost";
$user = "root";
$pass = "";
$db ="eeg";
$koneksi = mysql_connect($host, $user, $pass);
if (!$koneksi) {
echo "Koneksi ke server tidak berhasil";
};
$database = mysql_select_db($db);
if (!$database) {
echo "Koneksi ke database tidak berhasil";
}
mysql_select_db($db) or die ("Database not Found !");
$carisesi= $_SESSION['carisesimedian'] ;
$sql = "SELECT COUNT(sesi) FROM gelombang where sesi='$carisesi'"; 
$rs_result = mysql_query($sql); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 
for ($i=0; $i<=$total_records; $i++) { 


$batas=10;
$menu = array();
$menu1 = array();
$menu2 = array();
$menu3 = array();
$menu4 = array();
$menu5 = array();
$menu6 = array();
$sql = "SELECT * FROM gelombang where sesi='$carisesi' LIMIT $i,$batas"; 
$rs_result = mysql_query ($sql); 

while( $rows = mysql_fetch_row($rs_result) ){
    
			 $menu[] = $rows[5];
			$menu1[] = $rows[6];
			$menu2[] = $rows[7];
			$menu3[] = $rows[8];
			$menu4[] = $rows[9];
			$menu5[] = $rows[10];
			$menu6[] = $rows[11];
			
				
			

			
    
    ?>
	
		  
	
  
  
	<tbody>
          <tr>
            <td>
<?php


}

$theta = calculate_median($menu);
$alpha = calculate_median($menu1);
$beta = calculate_median($menu2);
$low_alpha = calculate_median($menu3);
$high_alpha = calculate_median($menu4);
$low_beta = calculate_median($menu5);
$high_beta = calculate_median($menu6);
 
 


$sql = "INSERT INTO median
VALUES (' ','$i','$theta','$alpha','$beta','$low_alpha','$high_alpha','$low_beta','$high_beta','$carisesi')"; 
$rs_result = mysql_query ($sql); 
}




?>
</td>
          </tr>
            
         
		  
		  </table>
		  








